<template>
  <div class="about">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>人事管理</el-breadcrumb-item>
      <el-breadcrumb-item>考勤管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索框按钮 -->
      <el-form :inline="true" ref="info" :model="info">
        <el-form-item prop="jobNumber">
          <el-input
            v-model="info.jobNumber"
            style="width: 160px"
            placeholder="请输入员工工号"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="startTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.startTime"
            start-placeholder="开始日期"
            placeholder="请选择开始时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item prop="endTime">
          <el-date-picker
            style="width: 160px"
            v-model="info.endTime"
            end-placeholder="结束日期"
            placeholder="请选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            size="medium"
            round
            @click="resetForm('info')"
            >重置</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getUserList" size="medium" round
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <el-row>
        <el-col :span="4"
          ><el-button type="primary" @click="showTime"
            >设置查询上下班时间</el-button
          ></el-col
        >
      </el-row>
      <!-- 打卡信息列表 -->
      <el-table border stripe :data="userList">
        <el-table-column label="序号" type="index"> </el-table-column>
        <el-table-column label="工号" prop="jobNumber"> </el-table-column>
        <el-table-column label="考勤日期" prop="date"> </el-table-column>
        <el-table-column label="打卡类型" prop="clockType"> </el-table-column>
        <el-table-column label="拜访内容" prop="visitContent">
        </el-table-column>
        <el-table-column
          label="上班打卡"
          prop="workClockTime"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="下班打卡" prop="leaveClockTime">
        </el-table-column>
        <el-table-column
          label="打卡地址"
          prop="clockAddress"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="打卡备注" prop="remark"> </el-table-column>
        <el-table-column label="打卡设备" prop="clockEquipment">
        </el-table-column>
        <el-table-column label="打卡状态" prop="clockStatus"> </el-table-column>
        <el-table-column label="操作" width="145px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="showEditDialog(scope.row.id)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="removeId(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="info.currentPage"
        :page-sizes="[2, 8, 15, 100]"
        :page-size="info.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 更新编辑时间信息 -->
      <el-dialog
        title="更新编辑打卡信息"
        :visible.sync="editDialogVisible"
        width="30%"
        @close="editDialogClosed"
      >
        <el-form
          :model="editForm"
          :rules="editFormRules"
          ref="editFormRef"
          label-width="110px"
        >
          <el-form-item label="考勤日期" prop="date">
            <el-input v-model="editForm.date"></el-input>
          </el-form-item>
          <el-form-item label="上班打卡时间" prop="workClockTime">
            <el-input v-model="editForm.workClockTime"></el-input>
          </el-form-item>
          <el-form-item label="下班打卡时间" prop="leaveClockTime">
            <el-input v-model="editForm.leaveClockTime"></el-input>
          </el-form-item>
          <el-form-item label="打卡地址" prop="clockAddress">
            <el-input v-model="editForm.clockAddress"></el-input>
          </el-form-item>
          <el-form-item label="打卡备注" prop="remark">
            <el-input v-model="editForm.remark"></el-input>
          </el-form-item>
          <el-form-item label="打卡设备" prop="clockEquipment">
            <el-input v-model="editForm.clockEquipment"></el-input>
          </el-form-item>
          <el-form-item label="打卡状态" prop="clockStatus">
            <el-input v-model="editForm.clockStatus"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editInfo">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 查询编辑上下班时间 -->
      <el-dialog
        title="查询编辑上下班时间信息"
        :visible.sync="timeDialogVisible"
        width="35%"
        @close="timeDialogClosed"
      >
        <el-form
          :model="timeForm"
          :rules="timeFormRules"
          ref="timeFormRef"
          label-width="110px"
        >
          <el-form-item label="下班时间" prop="leaveTime">
            <el-input v-model="timeForm.leaveTime"></el-input>
          </el-form-item>
          <el-form-item label="上班时间" prop="workTime">
            <el-input v-model="timeForm.workTime"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="timeDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editTime">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    return {
      info: {
        currentPage: 1,
        size: 4,
        jobNumber: "",
        startTime: "",
        endTime: "",
      },
      userList: [],
      total: 0,
      //编辑打卡信息对话框隐藏
      editDialogVisible: false,
      //查询到的打卡信息对象
      editForm: {},
      //编辑打卡信息规则
      editFormRules: {
        department: [
          { required: true, message: "请输入部门", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "备注名称为1~50个字符之间",
            trigger: "blur",
          },
        ],
        employeeName: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          {
            min: 1,
            max: 5,
            message: "备注名称为1~5个字符之间",
            trigger: "blur",
          },
        ],
        date: [{ required: true, message: "请输入考勤日期", trigger: "blur" }],

        workClockTime: [
          { required: true, message: "请输入上班打卡时间", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "备注名称为2~50个字符之间",
            trigger: "blur",
          },
        ],
        leaveClockTime: [
          { required: true, message: "请输入下班打卡时间", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "备注名称为2~50个字符之间",
            trigger: "blur",
          },
        ],
        clockAddress: [
          { required: true, message: "请输入打卡地址", trigger: "blur" },
          {
            min: 2,
            max: 100,
            message: "备注名称为2~100个字符之间",
            trigger: "blur",
          },
        ],
        remark: [
          { required: true, message: "请输入打卡备注", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "备注名称为2~500个字符之间",
            trigger: "blur",
          },
        ],
        clockEquipment: [
          { required: true, message: "请输入打卡设备", trigger: "blur" },
          {
            min: 2,
            max: 10,
            message: "备注名称为2~10个字符之间",
            trigger: "blur",
          },
        ],
        clockStatus: [
          { required: true, message: "请输入打卡状态", trigger: "blur" },
          {
            min: 2,
            max: 5,
            message: "备注名称为2~2个字符之间",
            trigger: "blur",
          },
        ],
      },
      //编辑时间信息对话框隐藏
      timeDialogVisible: false,
      //查询到的时间信息对象
      timeForm: {},
      //编辑打卡信息规则
      timeFormRules: {
        workTime: [
          { required: true, message: "请输入上班时间", trigger: "blur" },
        ],
        leaveTime: [
          { required: true, message: "请输入下班时间", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //获取打卡信息
    async getUserList() {
      await this.$http
        .get("admin/adminSelectAttendance", { params: this.info })
        .then((res) => {
          console.log(res);
          if (res.data.flag !== true) {
            return this.$message.error("获取打卡信息失败!");
          }
          this.userList = res.data.rows;
          this.total = res.data.total;
        });
    },
    //监听信息条数
    handleSizeChange(newSize) {
      // console.log(newSize);
      this.info.size = newSize;
      this.getUserList();
    },
    //监听页数
    handleCurrentChange(newPage) {
      // console.log(newPage);
      this.info.currentPage = newPage;
      this.getUserList();
    },
    //更新编辑打卡信息打卡
    async showEditDialog(id) {
      console.log(id);
      const { data: res } = await this.$http.get(
        "admin/adminSelectAttendanceById",
        { params: { id } }
      );
      if (res.flag !== true) {
        return this.$message.error("获取打卡信息失败!");
      }
      console.log(res);
      this.editForm = res.data;
      this.editDialogVisible = true;
    },
    // 监听更新打卡对话框的关闭事件
    editDialogClosed() {
      this.$refs.editFormRef.resetFields();
    },
    //更新考勤信息并提交
    editInfo() {
      this.$refs.editFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("admin/updateAttendance", {
          id: this.editForm.id,
          department: this.editForm.department,
          employeeName: this.editForm.employeeName,
          date: this.editForm.date,
          workClockTime: this.editForm.workClockTime,
          leaveClockTime: this.editForm.leaveClockTime,
          clockAddress: this.editForm.clockAddress,
          remark: this.editForm.remark,
          clockEquipment: this.editForm.clockEquipment,
          clockStatus: this.editForm.clockStatus,
        });
        if (res.flag !== true) {
          return this.$message.error("更新打卡信息失败");
        }
        //关闭对话框
        this.editDialogVisible = false;
        //提示更新考勤信息成功
        this.$message.success("更新打卡信息成功");
        //获取新的更新考勤信息
        this.getUserList();
      });
    },
    //删除考勤信息
    async removeId(id) {
      console.log(id);
      const confirmResult = await this.$confirm(
        "此操作将永久删除该考勤信息, 是否继续?",
        "删除考勤信息",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmResult !== "confirm") {
        return this.$message.info("已经取消删除！");
      }
      const { data: res } = await this.$http.post(
        "admin/deleteAttendance/",
        qs.stringify({ id: id }),
        {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded",
          },
        }
      );
      console.log(res);

      if (res.flag !== true) {
        return this.$message.error("删除考勤信息失败");
      }
      this.$message.success("删除考勤信息成功");
      this.getUserList();
    },
    //查询上下班时间
    async showTime() {
      const { data: res } = await this.$http.get("admin/selectClockTime");
      console.log(res);
      if (res.flag !== true) {
        return this.$message.error("获取时间信息失败!");
      }

      this.timeForm = res.data[0];
      this.timeDialogVisible = true;
    },
    // 监听更新时间对话框的关闭事件
    timeDialogClosed() {
      this.$refs.timeFormRef.resetFields();
    },
    //提交修改时间
    editTime() {
      this.$refs.timeFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("admin/updateClockTime", {
          id: this.timeForm.id,
          leaveTime: this.timeForm.leaveTime,
          workTime: this.timeForm.workTime,
        });
        if (res.flag !== true) {
          return this.$message.error("更新时间信息失败");
        }
        //关闭对话框
        this.timeDialogVisible = false;
        //提示更新考勤信息成功
        this.$message.success("更新时间信息成功");
        //获取新的更新考勤信息
        this.getUserList();
      });
    },
  },
};
</script>

<style></style>
